package com.cl.service.impl;

import com.cl.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.List;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cl.utils.PageUtils;
import com.cl.utils.Query;


import com.cl.dao.ExamrecordDao;
import com.cl.entity.ExamrecordEntity;
import com.cl.service.ExamrecordService;
import com.cl.entity.view.ExamrecordView;

@Service("examrecordService")
public class ExamrecordServiceImpl extends ServiceImpl<ExamrecordDao, ExamrecordEntity> implements ExamrecordService {

    @Override
    public PageUtils queryPageGroupBy(Map<String, Object> params, Wrapper<ExamrecordEntity> wrapper) {
        Page<ExamrecordView> page = new Query<ExamrecordView>(params).getPage();
        page.setRecords(baseMapper.selectGroupBy(page, wrapper));
        PageUtils pageUtil = new PageUtils(page);
        return pageUtil;
    }

    @Override
    public PageUtils queryPageOptionsNum(Map<String, Object> params, Wrapper<ExamrecordEntity> wrapper) {
        Page<ExamrecordView> page = new Query<ExamrecordView>(params).getPage();
        page.setRecords(baseMapper.selectOptionsNum(page, wrapper));
        PageUtils pageUtil = new PageUtils(page);
        return pageUtil;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<ExamrecordEntity> page = this.selectPage(
                new Query<ExamrecordEntity>(params).getPage(),
                new EntityWrapper<ExamrecordEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Wrapper<ExamrecordEntity> wrapper) {
        Page<ExamrecordView> page = new Query<ExamrecordView>(params).getPage();
        page.setRecords(baseMapper.selectListView(page, wrapper));
        PageUtils pageUtil = new PageUtils(page);
        return pageUtil;
    }

    @Override
    public List<ExamrecordView> selectListView(Wrapper<ExamrecordEntity> wrapper) {
        return baseMapper.selectListView(wrapper);
    }

    @Override
    public ExamrecordView selectView(Wrapper<ExamrecordEntity> wrapper) {
        return baseMapper.selectView(wrapper);
    }

    @Override
    public R updateScoreByExam(String userid, String paperid, String questionid, Long newScore) {
        if(StringUtils.isNotEmpty(userid) && StringUtils.isNotEmpty(paperid)){
            ExamrecordEntity exam = baseMapper.selectPaper(userid,paperid,questionid);
            if(exam != null ){
                exam.setMyscore(newScore);
                baseMapper.updateById(exam);
                return R.ok();
            }
        }
        return  R.ok();
    }


}
